package cn.edu.sdjzu.xg.kcsj.service;

import cn.edu.sdjzu.xg.kcsj.dao.DictionaryDao;
import cn.edu.sdjzu.xg.kcsj.domain.Dictionary;
import cn.edu.sdjzu.xg.kcsj.exception.BysjException;
import cn.edu.sdjzu.xg.kcsj.util.JdbcHelper;
import cn.edu.sdjzu.xg.kcsj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public class DictionaryService {
    private static DictionaryDao dictionaryDao = DictionaryDao.getInstance();
    private static DictionaryService dictionaryService = new DictionaryService();

    public static DictionaryService getInstance(){
        return dictionaryService;
    }

    // 查找全部院系
    public Collection<Dictionary> findAll(List conditionList, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<Dictionary> dictionaries = new TreeSet<>();
        try {
            dictionaries = dictionaryDao.findAll(conditionList,pagination,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return dictionaries;
    }

    // 按id查找院系
    public Dictionary find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Dictionary dictionary = null;
        try {
            dictionary = dictionaryDao.find(id,connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return dictionary;
    }

    public Collection<Dictionary> findA() throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<Dictionary> dictionaries = new TreeSet<>();
        try {
            dictionaries = dictionaryDao.findA(connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return dictionaries;
    }

    // 更新一条
    public boolean update(Dictionary dictionary) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean updated =false;
        try {
            updated = dictionaryDao.update(dictionary,connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(Dictionary dictionary) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean added =false;
        try {
            added = dictionaryDao.add(dictionary,connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }

    // 删除
    public boolean delete(Integer id) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean deleted = false;
        try {
            deleted = dictionaryDao.delete(id,connection);
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }
}
